/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.control;

import de.marantis.app.wow.gfi.beans.GuildCharacterBean;
import de.marantis.app.wow.gfi.beans.ReputationBean;
import de.marantis.app.wow.gfi.helper.ArmoryReputationHelper;
import de.marantis.app.wow.gfi.helper.HttpHelper;
import java.net.URL;
import org.apache.log4j.Logger;

public abstract class ReputationThread extends Thread {

    private final static Logger log = Logger.getLogger(ReputationThread.class);
    
    private MainController ctrl;
    private GuildCharacterBean guildChar;
    private int actualIdx;
    private int completeCount;
    private HtmlLoadTypeEnum type;

    public ReputationThread(MainController ctrl, GuildCharacterBean guildChar, int completeCount, int actualIdx, HtmlLoadTypeEnum type) {
        this.ctrl = ctrl;
        this.guildChar = guildChar;
        this.actualIdx = actualIdx;
        this.completeCount = completeCount;
        this.type = type;
    }

    protected abstract void setStatus(String msg);
    protected abstract void fireReputationBeanUpdated(ReputationBean repBean);
    protected abstract void fireGuildCharUpdated(GuildCharacterBean guildChar, int idx);

    @Override
    public void run() {
        try {

            //*** character < level 10? (armory has no reputation informations about chars under lvl 10!!) ***
            if (this.guildChar.getCharLevel() < 10) {
                if (log.isDebugEnabled()){
                    log.debug("Character " + guildChar.getCharName() + " level < 10");
                }
                return;
            }

            String link = this.guildChar.getCharArmoryLink();
            if (link == null) {
                if (log.isDebugEnabled()){
                    log.debug("Character " + guildChar.getCharName() + " armory link ist NULL!");
                }
                return;
            }

            link = "http://eu.battle.net" + link + "reputation/tabular";

            //*** check the armory link ***
            if (!link.startsWith("http://eu.battle.net/wow/")) {
                guildChar.setLoadFlag(true);
                guildChar.setErrorFlag(true);
                log.error("Character " + guildChar.getCharName() + "ERROR LINK = " + link + ", IDX =" + actualIdx);
                this.fireGuildCharUpdated(guildChar, actualIdx);
                return;
            }

            if (this.type.equals(this.type.START_FIRST_TRY)) {
                StringBuffer sb = new StringBuffer("");
                sb.append("Lade Rufdetails aus dem Armory (");
                sb.append(actualIdx + 1);
                sb.append("/");
                sb.append(this.completeCount);
                sb.append(")");
                this.setStatus(sb.toString());

            } else if (this.type.equals(this.type.START_ERROR_RETRY)) {
                StringBuffer sb = new StringBuffer("");
                sb.append("Lade Rufdetails aufgrund von Fehlern erneut aus dem Armory (");
                sb.append(actualIdx + 1);
                sb.append("/");
                sb.append(this.completeCount);
                sb.append(")");
                this.setStatus(sb.toString());
            }

            //*** Load the html web page for this guild member ***
            URL aUrl = new URL(link);
            String html = HttpHelper.getHttpContent(aUrl);

            //*** Analyze the web page content for this guild member ***
            ReputationBean repBean = ArmoryReputationHelper.getReputation(html);

            //*** fire the update event ***
            this.fireReputationBeanUpdated(repBean);

            //*** set the reputation ***
            this.guildChar.setGuildReputation(repBean);

            //*** fire the update event ***
            this.fireGuildCharUpdated(this.guildChar, actualIdx);

        } catch (Exception e) {
            log.error(e);
        }
    }
}
